package org.zqh.ncc.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.PriorityBlockingQueue;

public class CommonThread<T> extends Thread {
    protected Logger log = LoggerFactory.getLogger(CommonThread.class);

    protected PriorityBlockingQueue<T> commonQueue;

    public boolean isStopping = false;
    public boolean isStopped = false;

    @Autowired
    protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    protected JdbcTemplate jdbcTemplate;

    public CommonThread(PriorityBlockingQueue<T> pbQueue) {
        this.commonQueue = pbQueue;
    }

    public CommonThread() {
    }

    @PostConstruct
    public void initMe() {
        log.info(getName() + " init...");
        this.start();
    }

    @PreDestroy
    public void destroyMe() {
        log.info(getName() + " begin destroy, queue size=" + commonQueue.size());
        while (!commonQueue.isEmpty()) {

        }
        interrupt();
        log.info(getName() + " destroy over!");
    }

    protected void beginSleep(long sleepTime) {
        try {
            Thread.sleep(sleepTime);
        } catch (Exception e) {
        }
    }

    public boolean isQueueEmpty() {
        return commonQueue.isEmpty();
    }

}
